<?php

namespace App\Models;

use Carbon\Carbon;
use DB;
use Illuminate\Database\Eloquent\Model;

class Team extends Model
{
    protected $guarded = [];

    public static function getTeamOrigin($date=null,$take=5){
        //$date= $date==null?Carbon::today()->toDateString():$date;
        if($date==null){
            if(self::where('arrive_date',Carbon::today()->toDateString())->first()){
                $date=Carbon::today()->toDateString();
            }else{
                $date = self::max('arrive_date');
            }
        }else
            $date = $date;
        $source = self::where('arrive_date',$date)
            ->select('origin', DB::raw('count(origin) as total'))
            ->groupBy('origin')
            ->take($take)
            ->orderByDesc('total')
            ->get();
        return $source;
    }

    public static function getSexRate($total=100){
        $teams = self::select(['sex'])->take($total)->orderByRaw('RAND()')->get();
        $man = count($teams->groupBy('sex')['男']);
        return ['man'=>$man,'woman'=>$total-$man];
    }

    public static function getCtsRate($date=null){
        $date=$date==null?Carbon::today()->toDateString():$date;
        $cts_rate = self::where('arrive_date',$date)
            ->select('travel_agency', DB::raw('count(travel_agency) as total'))
            ->groupBy('travel_agency')
            ->get();
        $cts_rate&&$cts_rate= self::where('arrive_date',self::max('arrive_date'))
            ->select('travel_agency', DB::raw('count(travel_agency) as total'))
            ->groupBy('travel_agency')
            ->get();
        return $cts_rate;
    }

    public static function getAgeRate($date=null){
        $date = $date==null?Carbon::today()->toDateString():$date;
        $list = self::where('arrive_date',$date)->orderByRaw('RAND()')->get(['id_card']);
        $list&&$list = self::where('arrive_date',self::max('arrive_date'))->orderByRaw('RAND()')->get(['id_card']);
        //$ageRate =
        $new = $list->map(function ($val){
            $age = get_age($val->id_card);
            if((int)$age<18)
                return '18岁以下';
            if((int)$age>=18&&(int)$age<30)
                return '18岁-30岁';
            if((int)$age>=30&&(int)$age<40)
                return '30岁-40岁';
            if((int)$age>=40&&(int)$age<60)
                return '40岁-60岁';
            if((int)$age>=18)
                return '60岁以上';
        })->sort()->toArray();
        return array_count_values($new);
    }
    public static function getSourceRate($date=null,$groupBy='province'){
        $date = $date==null?Carbon::today()->toDateString():$date;
        $list = self::where('arrive_date',$date)->get(['id_card']);
        $list&&$list = self::where('arrive_date',self::max('arrive_date'))->get(['id_card']);
//        $new =$list->map(function ($val){
//            if(strlen($val->id_card)>=8){
//                //$new = AdministrativeDivision::select(['province','city','county'])->where('code_six',substr($val->id_code,0,6))->first();
//                return get_source($val->id_card);
//            }
//        })->groupBy('province')->map(function($val){
//            $new = $val->groupBy('city')->map(function ($val){
//                return $val->groupBy('county');
//            });
//            return $new;
//        })->toArray();
        $new =$list->map(function ($val){
            if(strlen($val->id_card)>=8){
                return get_source($val->id_card);
            }
        })->groupBy($groupBy)->map(function ($val){
            return $val->count();
        })->sortByDesc(function ($val){
            return $val;
        })->toArray();
        return $new;
    }

    public static function getTodayTeamCount(){
        $count = self::where('arrive_date',Carbon::today()->toDateString())->groupBy('team_id')->count();
        $count==0&&$count = self::where('arrive_date',self::max('arrive_date'))->groupBy('team_id')->count();
        return $count;
    }

    public static function getTodayPersonCount(){
        $count = self::where('arrive_date',Carbon::today()->toDateString())->count();
        $count==0&&$count = self::where('arrive_date',self::max('arrive_date'))->count();
        return $count;
    }

    public static function teamNumTrend($type='month',$limit=6){
        $x = [];
        $y=[];
        $arg = [
            'month'=>'Y-m',
            'year'=>'Y',
            'day'=>'Y-m-d'
        ];
        for ($i=$limit-1;$i>=0;$i--){
            $time = date($arg[$type],strtotime("-".$i." ".$type));
            array_push($x,$time);
            $count = Team::where('arrive_date','like','%'.$time.'%')->count();
            array_push($y,$count);
        }
        return ['x'=>$x,'y'=>$y];
    }
}
